
/**
 * @file:       index.cpp
 * @author:     WubinWang
 * @contact:    wubinstu@163.com
 * @date:       2025-01-01
 * @license:    MIT License
 *
 * Copyright (c) 2025 WubinWang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * DISCLAIMER:
 * 1. User acknowledges all application risks and consequences arising from usage
 *    of this software are exclusively borne by the user.
 * 2. The original author shall not be held liable for any direct, indirect,
 *    incidental, special, exemplary or consequential damages.
 * 3. No warranty of fitness for particular purpose or non-infringement is provided. */


#include "index.hpp"

Advanced_Circular_Stepper_ct::Advanced_Circular_Stepper_ct (const Advanced_Circular_Stepper_ct & _other) noexcept {
#if defined(USE_ATOMIC) && (USE_ATOMIC == 1)
    this->_m_index_floor      = _other._m_index_floor.load ();
    this->_m_index_ceiling    = _other._m_index_ceiling.load ();
    this->_m_stepper_interval = _other._m_stepper_interval.load ();
    this->_m_index_location   = _other._m_index_location.load ();
#else
    this->_m_index_floor      = _other._m_index_floor;
    this->_m_index_ceiling    = _other._m_index_ceiling;
    this->_m_stepper_interval = _other._m_stepper_interval;
    this->_m_index_location   = _other._m_index_location;
#endif
}

Advanced_Circular_Stepper_ct::Advanced_Circular_Stepper_ct (Advanced_Circular_Stepper_ct && _other) noexcept {
#if defined(USE_ATOMIC) && (USE_ATOMIC == 1)
    this->_m_index_floor      = _other._m_index_floor.load ();
    this->_m_index_ceiling    = _other._m_index_ceiling.load ();
    this->_m_stepper_interval = _other._m_stepper_interval.load ();
    this->_m_index_location   = _other._m_index_location.load ();
#else
    this->_m_index_floor      = _other._m_index_floor;
    this->_m_index_ceiling    = _other._m_index_ceiling;
    this->_m_stepper_interval = _other._m_stepper_interval;
    this->_m_index_location   = _other._m_index_location;
#endif
    _other._m_index_floor      = 0;
    _other._m_index_ceiling    = 100;
    _other._m_stepper_interval = 1;
    _other._m_index_location   = 0;
}

Advanced_Circular_Stepper_ct & Advanced_Circular_Stepper_ct::operator= (const Advanced_Circular_Stepper_ct & _other) noexcept {
    if (this != &_other) {
#if defined(USE_ATOMIC) && (USE_ATOMIC == 1)
        this->_m_index_floor      = _other._m_index_floor.load ();
        this->_m_index_ceiling    = _other._m_index_ceiling.load ();
        this->_m_stepper_interval = _other._m_stepper_interval.load ();
        this->_m_index_location   = _other._m_index_location.load ();
#else
        this->_m_index_floor      = _other._m_index_floor;
        this->_m_index_ceiling    = _other._m_index_ceiling;
        this->_m_stepper_interval = _other._m_stepper_interval;
        this->_m_index_location   = _other._m_index_location;
#endif
    }
    return *this;
}

Advanced_Circular_Stepper_ct & Advanced_Circular_Stepper_ct::operator= (Advanced_Circular_Stepper_ct && _other) noexcept {
    if (this != &_other) {
#if defined(USE_ATOMIC) && (USE_ATOMIC == 1)
        this->_m_index_floor      = _other._m_index_floor.load ();
        this->_m_index_ceiling    = _other._m_index_ceiling.load ();
        this->_m_stepper_interval = _other._m_stepper_interval.load ();
        this->_m_index_location   = _other._m_index_location.load ();
#else
        this->_m_index_floor      = _other._m_index_floor;
        this->_m_index_ceiling    = _other._m_index_ceiling;
        this->_m_stepper_interval = _other._m_stepper_interval;
        this->_m_index_location   = _other._m_index_location;
#endif

        _other._m_index_floor      = 0;
        _other._m_index_ceiling    = 100;
        _other._m_stepper_interval = 1;
        _other._m_index_location   = 0;
    }
    return *this;
}

Advanced_Circular_Stepper_ct::Advanced_Circular_Stepper_ct (std::int64_t _floor, std::int64_t _ceiling, std::int64_t _interval) noexcept {
    if ((_floor < _ceiling) && (_interval < (_ceiling - _floor))) {
        this->_m_index_floor      = _floor;
        this->_m_index_ceiling    = _ceiling;
        this->_m_stepper_interval = _interval;
    }
#if defined(USE_ATOMIC) && (USE_ATOMIC == 1)
    this->_m_index_location = this->_m_index_floor.load ();
#else
    this->_m_index_location = this->_m_index_floor;
#endif
}

Advanced_Circular_Stepper_ct::~Advanced_Circular_Stepper_ct () noexcept {
    this->_m_index_floor      = 0;
    this->_m_index_ceiling    = 0;
    this->_m_stepper_interval = 0;
    this->_m_index_location   = 0;
}

Advanced_Circular_Stepper_ct::operator std::int64_t () const noexcept {
    return static_cast<std::int64_t> (this->_m_index_location & 0XFF'FF'FF'FF'FF'FF'FF'FF);
}

Advanced_Circular_Stepper_ct::operator std::int32_t () const noexcept {
    return static_cast<std::int32_t> (this->_m_index_location & 0X00'00'00'00'FF'FF'FF'FF);
}

Advanced_Circular_Stepper_ct::operator std::int16_t () const noexcept {
    return static_cast<std::int16_t> (this->_m_index_location & 0X00'00'00'00'00'00'FF'FF);
}

Advanced_Circular_Stepper_ct::operator std::int8_t () const noexcept {
    return static_cast<std::int8_t> (this->_m_index_location & 0X00'00'00'00'00'00'00'FF);
}

auto Advanced_Circular_Stepper_ct::operator= (std::int64_t _index) noexcept -> std::int64_t {
    if (_index >= this->_m_index_floor && _index < this->_m_index_ceiling) {
        this->_m_index_location = _index;
        return _index;
    }
    return -1;
}

auto Advanced_Circular_Stepper_ct::operator= (std::int32_t _index) noexcept -> std::int32_t {
    if (_index >= this->_m_index_floor && _index < this->_m_index_ceiling) {
        this->_m_index_location = _index;
        return _index;
    }
    return -1;
}

auto Advanced_Circular_Stepper_ct::operator= (std::int16_t _index) noexcept -> std::int16_t {
    if (_index >= this->_m_index_floor && _index < this->_m_index_ceiling) {
        this->_m_index_location = _index;
        return _index;
    }
    return -1;
}

auto Advanced_Circular_Stepper_ct::operator= (std::int8_t _index) noexcept -> std::int8_t {
    if (_index >= this->_m_index_floor && _index < this->_m_index_ceiling) {
        this->_m_index_location = _index;
        return _index;
    }
    return -1;
}

auto Advanced_Circular_Stepper_ct::operator+ (std::int64_t _increment) noexcept -> std::int64_t {
    _increment           %= (this->_m_index_ceiling - this->_m_index_floor);
    std::int64_t _result  = this->_m_index_location + _increment;
    if (_result >= this->_m_index_ceiling)
        _result = this->_m_index_floor + (_result - this->_m_index_ceiling);
    else if (_result < this->_m_index_floor)
        _result = this->_m_index_ceiling - (this->_m_index_floor - _result);
    return _result;
}

auto Advanced_Circular_Stepper_ct::operator+ (std::int32_t _increment) noexcept -> std::int32_t {
    return static_cast<std::int32_t> (this->operator+ (static_cast<std::int64_t> (_increment)) & 0X00'00'00'00'FF'FF'FF'FF);
}

auto Advanced_Circular_Stepper_ct::operator+ (std::int16_t _increment) noexcept -> std::int16_t {
    return static_cast<std::int16_t> (this->operator+ (static_cast<std::int64_t> (_increment)) & 0X00'00'00'00'00'00'FF'FF);
}

auto Advanced_Circular_Stepper_ct::operator+ (std::int8_t _increment) noexcept -> std::int8_t {
    return static_cast<std::int8_t> (this->operator+ (static_cast<std::int64_t> (_increment)) & 0X00'00'00'00'00'00'00'FF);
}

auto Advanced_Circular_Stepper_ct::operator- (std::int64_t _decrement) noexcept -> std::int64_t {
    _decrement           %= (this->_m_index_ceiling - this->_m_index_floor);
    std::int64_t _result  = this->_m_index_location - _decrement;
    if (_result < this->_m_index_floor)
        _result = this->_m_index_ceiling - (this->_m_index_floor - _result);
    else if (_result >= this->_m_index_ceiling)
        _result = this->_m_index_floor + (_result - this->_m_index_ceiling);
    return _result;
}

auto Advanced_Circular_Stepper_ct::operator- (std::int32_t _decrement) noexcept -> std::int32_t {
    return static_cast<std::int32_t> (this->operator- (static_cast<std::int64_t> (_decrement)) & 0X00'00'00'00'FF'FF'FF'FF);
}

auto Advanced_Circular_Stepper_ct::operator- (std::int16_t _decrement) noexcept -> std::int16_t {
    return static_cast<std::int16_t> (this->operator- (static_cast<std::int64_t> (_decrement)) & 0X00'00'00'00'00'00'FF'FF);
}

auto Advanced_Circular_Stepper_ct::operator- (std::int8_t _decrement) noexcept -> std::int8_t {
    return static_cast<std::int16_t> (this->operator- (static_cast<std::int64_t> (_decrement)) & 0X00'00'00'00'00'00'00'FF);
}

auto Advanced_Circular_Stepper_ct::operator++ () noexcept -> std::int64_t {
    this->_m_index_location += this->_m_stepper_interval;
    if (this->_m_index_location >= this->_m_index_ceiling)
        this->_m_index_location = this->_m_index_floor + (this->_m_index_location - this->_m_index_ceiling);
    return this->_m_index_location;
}

auto Advanced_Circular_Stepper_ct::operator-- () noexcept -> std::int64_t {
    this->_m_index_location -= this->_m_stepper_interval;
    if (this->_m_index_location < this->_m_index_floor)
        this->_m_index_location = this->_m_index_ceiling - (this->_m_index_floor - this->_m_index_location);
    return this->_m_index_location;
}

auto Advanced_Circular_Stepper_ct::operator++ (int) noexcept -> std::int64_t {
    std::int64_t _old_index_location  = this->_m_index_location;
    this->_m_index_location          += this->_m_stepper_interval;
    if (this->_m_index_location >= this->_m_index_ceiling)
        this->_m_index_location = this->_m_index_floor + (this->_m_index_location - this->_m_index_ceiling);
    return _old_index_location;
}

auto Advanced_Circular_Stepper_ct::operator-- (int) noexcept -> std::int64_t {
    std::int64_t _old_index_location  = this->_m_index_location;
    this->_m_index_location          -= this->_m_stepper_interval;
    if (this->_m_index_location < this->_m_index_floor)
        this->_m_index_location = this->_m_index_ceiling - (this->_m_index_floor - this->_m_index_location);
    return _old_index_location;
}

auto Advanced_Circular_Stepper_ct::operator+= (std::int64_t _increment) noexcept -> Advanced_Circular_Stepper_ct & {
    this->operator= (this->operator+ (_increment));
    return *this;
}

auto Advanced_Circular_Stepper_ct::operator+= (std::int32_t _increment) noexcept -> Advanced_Circular_Stepper_ct & {
    this->operator= (this->operator+ (_increment));
    return *this;
}

auto Advanced_Circular_Stepper_ct::operator+= (std::int16_t _increment) noexcept -> Advanced_Circular_Stepper_ct & {
    this->operator= (this->operator+ (_increment));
    return *this;
}

auto Advanced_Circular_Stepper_ct::operator+= (std::int8_t _increment) noexcept -> Advanced_Circular_Stepper_ct & {
    this->operator= (this->operator+ (_increment));
    return *this;
}

auto Advanced_Circular_Stepper_ct::operator-= (std::int64_t _decrement) noexcept -> Advanced_Circular_Stepper_ct & {
    this->operator= (this->operator- (_decrement));
    return *this;
}

auto Advanced_Circular_Stepper_ct::operator-= (std::int32_t _decrement) noexcept -> Advanced_Circular_Stepper_ct & {
    this->operator= (this->operator- (_decrement));
    return *this;
}

auto Advanced_Circular_Stepper_ct::operator-= (std::int16_t _decrement) noexcept -> Advanced_Circular_Stepper_ct & {
    this->operator= (this->operator- (_decrement));
    return *this;
}

auto Advanced_Circular_Stepper_ct::operator-= (std::int8_t _decrement) noexcept -> Advanced_Circular_Stepper_ct & {
    this->operator= (this->operator- (_decrement));
    return *this;
}

auto Advanced_Circular_Stepper_ct::operator== (const Advanced_Circular_Stepper_ct & _other) noexcept -> bool {
    return this->_m_index_location == _other._m_index_location;
}

auto Advanced_Circular_Stepper_ct::operator!= (const Advanced_Circular_Stepper_ct & _other) noexcept -> bool {
    return this->_m_index_location != _other._m_index_location;
}

auto Advanced_Circular_Stepper_ct::operator== (std::int64_t _other) noexcept -> bool {
    return this->_m_index_location == _other;
}

auto Advanced_Circular_Stepper_ct::operator!= (std::int64_t _other) noexcept -> bool {
    return this->_m_index_location != _other;
}

std::int64_t Advanced_Circular_Stepper_ct::inc () noexcept {
    return this->operator++ (0);
}

std::int64_t Advanced_Circular_Stepper_ct::dec () noexcept {
    return this->operator-- (0);
}

auto Advanced_Circular_Stepper_ct::add (std::int64_t _increment) noexcept -> std::int64_t {
    return this->operator+ (_increment);
}

auto Advanced_Circular_Stepper_ct::sub (std::int64_t _decrement) noexcept -> std::int64_t {
    return this->operator- (_decrement);
}

auto Advanced_Circular_Stepper_ct::getFloor () const noexcept -> std::int64_t {
    return this->_m_index_floor;
}

auto Advanced_Circular_Stepper_ct::setFloor (std::int64_t _floor) noexcept -> bool {
    if (_floor >= this->_m_index_ceiling)
        return false;

    this->_m_index_floor = _floor;
    return true;
}

auto Advanced_Circular_Stepper_ct::getCeiling () const noexcept -> std::int64_t {
    return this->_m_index_ceiling;
}

auto Advanced_Circular_Stepper_ct::setCeiling (std::int64_t _ceiling) noexcept -> bool {
    if (_ceiling <= this->_m_index_floor)
        return false;

    this->_m_index_ceiling = _ceiling;
    return true;
}

auto Advanced_Circular_Stepper_ct::getInterval () const noexcept -> std::int64_t {
    return this->_m_stepper_interval;
}

auto Advanced_Circular_Stepper_ct::setInterval (std::int64_t _interval) noexcept -> bool {
    if (_interval >= (this->_m_index_ceiling - this->_m_index_floor))
        return false;

    this->_m_stepper_interval = _interval;
    return true;
}

auto Advanced_Circular_Stepper_ct::getIndex () const noexcept -> std::int64_t {
    return this->_m_index_location;
}
